package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallRechargeMapper;
import org.linlinjava.litemall.db.domain.LitemallRecharge;
import org.linlinjava.litemall.db.domain.LitemallRechargeExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallRechargeService {
    @Resource
    private LitemallRechargeMapper rechargeMapper;

    public List<LitemallRecharge> queryByUid(Integer uid) {
        LitemallRechargeExample example = new LitemallRechargeExample();
        example.or().andUserIdEqualTo(uid).andDeletedEqualTo(false);
        return rechargeMapper.selectByExample(example);
    }

    public LitemallRecharge query(Integer userId, Integer id) {
        LitemallRechargeExample example = new LitemallRechargeExample();
        example.or().andIdEqualTo(id).andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return rechargeMapper.selectOneByExample(example);
    }

    public int add(LitemallRecharge recharge) {
        recharge.setAddTime(LocalDateTime.now());
        recharge.setUpdateTime(LocalDateTime.now());
        return rechargeMapper.insertSelective(recharge);
    }

    public int update(LitemallRecharge recharge) {
        recharge.setUpdateTime(LocalDateTime.now());
        return rechargeMapper.updateByPrimaryKeySelective(recharge);
    }

    public void delete(Integer id) {
        rechargeMapper.logicalDeleteByPrimaryKey(id);
    }

    public LitemallRecharge findDefault(Integer userId) {
        LitemallRechargeExample example = new LitemallRechargeExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        return rechargeMapper.selectOneByExample(example);
    }

    public void resetDefault(Integer userId) {
        LitemallRecharge recharge = new LitemallRecharge();
        recharge.setUpdateTime(LocalDateTime.now());
        LitemallRechargeExample example = new LitemallRechargeExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        rechargeMapper.updateByExampleSelective(recharge, example);
    }

    public List<LitemallRecharge> querySelective(Integer userId, Integer page, Integer limit, String sort, String order) {
        return querySelective(userId, null, page, limit, sort, order);
    }

    public List<LitemallRecharge> querySelective(Integer userId, String mobile, Integer page, Integer limit, String sort, String order) {
        LitemallRechargeExample example = new LitemallRechargeExample();
        LitemallRechargeExample.Criteria criteria = example.createCriteria();

        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return rechargeMapper.selectByExample(example);
    }
}
